package com.hubei.car.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubei.car.dto.req.PageCondition;
import com.hubei.car.entity.*;
import com.hubei.car.mapper.CarGoodsMapper;
import com.hubei.car.mapper.HbImgMapper;
import com.hubei.car.mapper.MsCouponMapper;
import com.hubei.car.mapper.MsProductMapper;
import com.hubei.car.service.CarGoodsService;
import com.hubei.car.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luokangtao
 * @create 2019-08-30 15:48
 */
@Service
@Slf4j
public class CarGoodsServiceImpl implements CarGoodsService {

    @Autowired
    private CarGoodsMapper goodsMapper;
    @Autowired
    private HbImgMapper hbImgMapper;
    @Autowired
    private MsProductMapper msProductMapper;
    @Autowired
    private MsCouponMapper msCouponMapper;

    @Override
    public String add(CarGoods carGoods) {
        carGoods.setCreateTime(LocalDateTime.now());
        int add = goodsMapper.insert(carGoods);
        if (add > 0) {
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public String update(CarGoods carGoods) {
        carGoods.setUpdateTime(LocalDateTime.now());
        int update = goodsMapper.updateById(carGoods);
        if (update > 0) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public String deleteOne(Long id) {
        int delete = goodsMapper.deleteById(id);
        if (delete > 0) {
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public CarGoods queryOne(Long id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public String updow(Long id, Integer updow) {
       int  updateUpdow =  goodsMapper.updateUpdow(id,updow);
       String updowStatus=null;
       if(updow==2){
           updowStatus="下架";
       }else {
           updowStatus ="上架";
       }
        if (updateUpdow > 0) {
            return updowStatus+"成功";
        }
        return updowStatus+"失败";
    }

    @Override
    public PageResult queryAll(PageCondition pageCondition) {
        //分页
        Page<CarGoods> page=new Page<>(pageCondition.getCurrent(),pageCondition.getSize());
        page.setAsc("horsepower");
        //查询条件
        QueryWrapper<CarGoods> querywrapper=new QueryWrapper<>();
        if(StrUtil.isNotBlank(pageCondition.getKeywordOne())){
            //汽车名称
            querywrapper.like("goodsname",pageCondition.getKeywordOne()).or().like("goodstype",pageCondition.getKeywordOne());
        }
        if(StrUtil.isNotBlank(pageCondition.getKeywordTwo())){
            //上下架: 0未上架 1上架 2下架
            querywrapper.eq("updow",pageCondition.getKeywordTwo());
        }
        IPage<CarGoods> carGoodsIPage = goodsMapper.selectPage(page, querywrapper);
        for (CarGoods record : carGoodsIPage.getRecords()) {
            //时间转换格式
            record.setCreateTimeString(DateUtil.localDateTimeToString(record.getCreateTime()));
            if(record.getUpdateTime()!=null){
                record.setUpdateTimeString(DateUtil.localDateTimeToString(record.getUpdateTime()));
            }
            //拼接时间字段
            if(StrUtil.isNotBlank(record.getEndTime())&&StrUtil.isNotBlank(record.getStartTime())){
                record.setStartEndTimeString(record.getStartTime()+"-"+record.getEndTime());
            }
        }
        return new PageResult(pageCondition.getCurrent(),pageCondition.getCurrent(),carGoodsIPage.getTotal(),carGoodsIPage.getRecords());
    }
    @Override
    public List<Map<String,Object>> queryRecommendImgs() {
        QueryWrapper<HbImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","imgname","imgurl","imgdetail","sort");
        queryWrapper.eq("imgtype", 1);
        queryWrapper.orderByAsc("sort");
        queryWrapper.last(" limit 9 ");
        List<Map<String,Object>> hbImgs = hbImgMapper.selectMaps(queryWrapper);
        return hbImgs;
    }

    @Override
    public PageResult queryCars(PageCondition pageCondition) {
        //分页
        Page<MsProduct> page = new Page<>(pageCondition.getCurrent(), pageCondition.getSize());
//        page.setAsc("horsepower");
        //查询条件
        QueryWrapper<MsProduct> querywrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(pageCondition.getKeywordOne())) {
            //汽车名称
            querywrapper.like("name", pageCondition.getKeywordOne());
        }
        IPage<MsProduct> msProductIPage = msProductMapper.selectPage(page, querywrapper);
        return new PageResult(pageCondition.getCurrent(), pageCondition.getCurrent(), msProductIPage.getTotal(), msProductIPage.getRecords());
    }

    @Override
    public PageResult queryCoupons(PageCondition pageCondition) {
        //分页
        Page<MsCoupon> page = new Page<>(pageCondition.getCurrent(), pageCondition.getSize());
        //查询条件
        QueryWrapper<MsCoupon> querywrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(pageCondition.getKeywordOne())) {
            //0 优惠券 1 团购券 2礼品券
            querywrapper.eq("enumtype", pageCondition.getKeywordOne());
        }
        querywrapper.eq("isstop", 0);
        IPage<MsCoupon> msCouponIPage = msCouponMapper.selectPage(page, querywrapper);
        for (MsCoupon record : msCouponIPage.getRecords()) {
            if (record.getEnumtype() != null) {
                switch (record.getEnumtype()) {
                    case 0: record.setEnumtypeString("优惠券");
                    case 1: record.setEnumtypeString("团购券");
                    case 2: record.setEnumtypeString("礼品券");
                }
            }
        }
        return new PageResult(pageCondition.getCurrent(), pageCondition.getCurrent(), msCouponIPage.getTotal(), msCouponIPage.getRecords());
    }
}
